package com.android.qjpmc.application_module.viewModel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.qjpmc.application_module.contract.HisCurveDataContract;
import com.android.qjpmc.application_module.model.HisDataModel;
import com.android.qjpmc.bean.GroupInfoPointDataBean;
import com.android.qjpmc.bean.PointTimeDataBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.GroupPointHisListBean;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.PointHistoryDataQueryBean;
import com.android.qjpmc.bean.StationDataBean;
import com.android.qjpmc.widget.mvvm.factory.CreateModel;
import com.android.qjpmc.widget.retrofithelper.rxschedulers.RxSchedulersHelper;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.android.qjpmc.widget.utils.ToastUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

@CreateModel(HisDataModel.class)
public class HisCurveDataViewModel extends HisCurveDataContract.ViewModel {
    @Override
    public void getPointListOfGroup(PwrDeviceBean data) {
        mModel.getPointListOfGroup(data.groupId, 1).subscribe(new ProgressObserver<List<PointParameterBean>>(false, this) {
            @Override
            public void _onNext(List<PointParameterBean> groupPointList) {
                //  getPointDataOfGroup(stationId, groupId,groupPointList);
                data.pointList = groupPointList;
                mView.returnGroupPointList(data);
            }
        });

    }


    public void getPointDataOfStation(StationDataBean stationData, Map<String, Object> map,
                                      Map<String, PointParameterBean> propsDataMap) {


        mModel.getPointHistoryDataOfStation(map).map(new Function<Object, List<GroupPointHisListBean>>() {
            @Override
            public List<GroupPointHisListBean> apply(Object o) throws Exception {
                if (o == null) {
                    ToastUtils.showShort("没有查询到数据");
                    return new ArrayList<>();
                }
                String s = JSONObject.toJSONString(o);
                Logger.d("getPointDataOfStation" + o.getClass().getSimpleName());
                JSONObject jsonObject = JSONObject.parseObject(s);
                List<String> deviceCodeList = (List<String>) map.get("deviceCodeList");
                List<String> colNameList = (List<String>) map.get("colNameList");
                String func = (String) map.get("func");
                List<GroupPointHisListBean> groupPointHisLists = new ArrayList<>();
                // 通过 deviceCode获取数据
                //一个设备中 只会有一个deviceCode
                String deviceCode = deviceCodeList.get(0);
                //    for (String deviceCode : deviceCodeList) {
                JSONArray deviceCodeArray = jsonObject.getJSONArray(deviceCode);
                if (deviceCodeArray != null) {
                    for (String colName : colNameList) {
                        //构建图表展示时 需要的数据
                        GroupPointHisListBean groupPointHisData = new GroupPointHisListBean();
                        groupPointHisData.colName = colName;
                        groupPointHisData.deviceCode = deviceCode;
                        groupPointHisData.propsName = propsDataMap.get(colName).propsName;
                        groupPointHisData.deviceName = propsDataMap.get(colName).deviceName;
                        List<PointTimeDataBean> pointValueList = new ArrayList<>();
                        //数据点的list数据源
                        groupPointHisData.pointValueList = pointValueList;
                        //每一项数据的key是是由func+colName组成的
                        String key = func + "(" + colName + ")";
                        for (int i = 0; i < deviceCodeArray.size(); i++) {
                            //
                            JSONObject deviceCodeJsonObject = deviceCodeArray.getJSONObject(i);
                            groupPointHisData.deviceCode = deviceCode;
                            //通过key获取到具体数据
                            Double pointValue = deviceCodeJsonObject.getDouble(key);
                            //获取开始时间
                            String pointStartTime = deviceCodeJsonObject.getString("_wstart");
                            //获取结束时间
                            String pointEndTime = deviceCodeJsonObject.getString("_wend");
                            PointTimeDataBean pointTimeDataBean = new PointTimeDataBean();
                            pointTimeDataBean.value = pointValue;
                            pointTimeDataBean.startTime = pointStartTime;
                            pointTimeDataBean.endTime = pointEndTime;
                            pointValueList.add(pointTimeDataBean);
                        }
                        groupPointHisLists.add(groupPointHisData);
                    }
                }
                // }
                Logger.d("groupPointHisLists" + groupPointHisLists.size());
                return groupPointHisLists;
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new ProgressObserver<List<GroupPointHisListBean>>(true, this) {
                    @Override
                    public void _onNext(List<GroupPointHisListBean> pointDataList) {
                        mView.returnGroupPointDataList(pointDataList);
                    }
                });


    }
}
